﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class RoomType
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}", Name, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        internal IList<ProjectRoomType> ProjectRoomTypes
        {
            get
            {
                return InternalProjectRoomTypes.Where(projectRoomType => projectRoomType.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Project> Projects
        {
            get
            {
                return InternalProjectRoomTypes.Where(projectRoomType => projectRoomType.IsDeleted == false && projectRoomType.Project.IsDeleted == false).Select(projectRoomType => projectRoomType.Project).ToList();
            }
        }

        #endregion

        #region Add & Remove ProjectRoomType

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal ProjectRoomType AddProjectRoomType(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var projectRoomType = new ProjectRoomType
                                      {
                                          RowId = Guid.NewGuid(),
                                          CreatedDate = DateTime.Now,
                                      };
            InternalProjectRoomTypes.Add(projectRoomType);
            projectRoomType.IsDeleted = false;
            projectRoomType.ModifiedDate = DateTime.Now;
            projectRoomType.ModifiedByUser = modifiedUser;
            return projectRoomType;
        }

        /// <summary>
        /// </summary>
        /// <param name = "projectRoomType"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveProjectRoomType(ProjectRoomType projectRoomType, User modifiedUser)
        {
            if (projectRoomType == null)
            {
                throw new ArgumentNullException("projectRoomType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalProjectRoomTypes.Contains(projectRoomType))
            {
                projectRoomType.Delete(modifiedUser);
            }
        }

        #endregion

        #region Set Projects

        /// <summary>
        /// </summary>
        /// <param name = "projects"></param>
        /// <param name = "modifiedUser"></param>
        public void SetProjects(IList<Project> projects, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (projects != null)
            {
                var projectRoomTypes = InternalProjectRoomTypes.ToList();
                projectRoomTypes.ForEach(projectRoomType => projectRoomType.Delete(modifiedUser));
                foreach (var project in projects.Distinct())
                {
                    var currentProjectRoomType = projectRoomTypes.FirstOrDefault(projectRoomType => projectRoomType.Project == project);
                    if (currentProjectRoomType != null)
                    {
                        currentProjectRoomType.CancelDelete(modifiedUser);
                    }
                    else
                    {
                        currentProjectRoomType = AddProjectRoomType(modifiedUser);
                        currentProjectRoomType.Project = project;
                    }
                }
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "RoomType", "Name"));
            }
            if (RoomsNumber < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "RoomType", "RoomsNumber"));
            }
            if (HallsNumber < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "RoomType", "HallsNumber"));
            }
            if (ToiletsNumber < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "RoomType", "ToiletsNumber"));
            }
            if (KitchensNumber < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "RoomType", "KitchensNumber"));
            }
            if (BalconiesNumber < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "RoomType", "BalconiesNumber"));
            }
            Name = Name.Length > 64 ? Name.Substring(0, 64) : Name;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<ProjectRoomType>) ProjectRoomTypes).ForEach(projectRoomType => projectRoomType.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}